package com.bawei.module_moto_circle.ui.post.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_moto_circle.R
import com.bawei.module_moto_circle.databinding.LayoutPostItemBinding
import com.bawei.module_moto_circle.databinding.LayoutRefreshRecyclerviewBinding
import com.bawei.module_moto_circle.model.PostInfo

/**
 *作者：lxy
 *功能:
 *时间：2025/6/3
 */
class PostListAdapter(

    private val onItemClick: ((PostInfo, Int) -> Unit)? = null,
    private val onUserClick: ((PostInfo) -> Unit)? = null,
//    private val onImageClickWithThumbnail: ((Int, Int, List<Image>, List<Info>) -> Unit)? = null,
    private val onShareClick: ((PostInfo) -> Unit)? = null,
    private val onCommentsClick: ((PostInfo) -> Unit)? = null,
    private val onLikeClick: ((PostInfo, Int) -> Unit)? = null,
    private val onMoreActionClick: ((PostInfo, Int) -> Unit)? = null,
    private val onLikeViewClick: ((PostInfo, Int, View) -> Unit)? = null,
    private val onCollectionViewClick: ((PostInfo, Int, View) -> Unit)? = null,
    private val onFollowViewClick: ((PostInfo, Int, View) -> Unit)? = null,
// 是否显示关系
    private val showRelationState: Boolean = true,
) : RecyclerView.Adapter<PostViewHolder>() {
    private val mData = mutableListOf<PostInfo>()

    fun setData(data: List<PostInfo>) {
        mData.clear()
        mData.addAll(data)
        val size = mData.size
        notifyItemRangeInserted(size - 1, data.size)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding =
            LayoutPostItemBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        return PostViewHolder(
            onItemClick = onItemClick,
            onUserClick = onUserClick,
//            onImageClickWithThumbnail = onImageClickWithThumbnail,
            onShareClick = onShareClick,
            onCommentsClick = onCommentsClick,
            onLikeClick = onLikeClick,
            onMoreActionClick = onMoreActionClick,
            onLikeViewClick = onLikeViewClick,
            onCollectionViewClick = onCollectionViewClick,
            onFollowViewClick = onFollowViewClick,
            itemView = binding.root,
            mBinding = binding
        )
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
        if (payloads.isEmpty()) {
            super.onBindViewHolder(holder, position, payloads) //注意！！
        } else {
            holder.mBinding.apply {
                if (mData[position].if_like == 1) {
                    ivLike.setImageResource(R.mipmap.icon_like_checked)
                } else {
                    ivLike.setImageResource(R.mipmap.icon_like)
                }
                if (mData[position].if_collect == 1) {
                    ivCollection.setImageResource(R.mipmap.icon_collection_checked)
                } else {
                    ivCollection.setImageResource(R.mipmap.icon_collection)
                }
                tvLike.text = mData[position].formatLikeNumber()
                tvCollection.text = mData[position].formatCollectionNumber()
                tvShare.text = mData[position].formatShareNumber()
            }
        }
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        mData[position].needShowFollowState = showRelationState
        //databinding数据绑定

        holder.mBinding.holder = holder
        holder.mBinding.post = mData[position]

        //自己处理部分UI绑定
        holder.initView(mData[position], position)

    }

    fun getItemByPosition(position: Int): PostInfo? {
        if (position < 0 || position >= mData.size) {
            return null
        }
        return mData[position]
    }

    /**
     * 局部增量刷新
     */
    fun updatePostItem(postItem: PostInfo) {
        repeat(itemCount) {
            val item = mData[it]
            if (item.posts_id == postItem.posts_id) {
                item.apply {
                    if_like = postItem.if_like
                    if_collect = postItem.if_collect
                    like_number = postItem.like_number
                    collect_number = postItem.collect_number
                    share_number = postItem.share_number
                    notifyItemChanged(it, "update")
                    return
                }
            }

        }

    }
}